#include "widget.h"
#include "ui_widget.h"

#include <QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->listWidget->addItem("刘备");
    ui->listWidget->addItem("孙权");
    ui->listWidget->addItem("张飞");
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    if (current != nullptr)
    {
        qDebug() << "当前选中" << current->text();
    }

    if (previous != nullptr)
    {
        qDebug() << "上一个" << previous->text();
    }
}

void Widget::on_pushButton_clicked()
{
    QString text = ui->lineEdit->text();
    if (text.isEmpty())
    {
        return;
    }

    ui->listWidget->addItem(text);
}

void Widget::on_pushButton_2_clicked()
{
    // 获取选中元素下标
    int row = ui->listWidget->currentRow();
    ui->listWidget->takeItem(row);
}
